package forum_system_test;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.IOException;

/**
 * @author tongchen
 * @create 2023-08-18 14:08
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LoginTest {
    private static ChromeDriver chromeDriver=TestUtils.getDriver();
    @BeforeAll
    public static void  openWeb(){
        chromeDriver.get("http://47.93.222.41:58080/sign-in.html");
    }
    @Order(1)
    @ParameterizedTest
    @CsvSource({"刘健磊,123456","张智怡,123456","xcl,'or 1 = 1'"})
    public void loginTest(String name,String password) throws IOException, InterruptedException {
        //再输入账号密码之前应该清空
        //进行账号密码的输入和点击登录操作
        WebElement element = chromeDriver.findElement(By.cssSelector("#username"));
        WebElement element1 = chromeDriver.findElement(By.cssSelector("#password"));
        WebElement element2 = chromeDriver.findElement(By.cssSelector("#submit"));
        element.clear();
        element1.clear();
        element.sendKeys(name);
        element1.sendKeys(password);
        element2.click();
        //强制等待，等待资源全部更新完成
        Thread.sleep(1000);
        //判断是否跳转成功
        if(!chromeDriver.getCurrentUrl().equals("http://47.93.222.41:58080/sign-in.html")){
            //等待资源全部加载完成
            //切换窗口，进行退出操作
            String windowHandle = chromeDriver.getWindowHandle();
            chromeDriver.switchTo().window(windowHandle);
            chromeDriver.findElement(By.cssSelector("#index_nav_avatar")).click();
            chromeDriver.findElement(By.cssSelector("#index_user_logout")).click();
            //再次切换句柄
            String windowHandleLogin = chromeDriver.getWindowHandle();
            chromeDriver.switchTo().window(windowHandleLogin);
        }
        //获取截图
        //TestUtils.getScreenShot();
    }
        @Order(2)
        @ParameterizedTest
        @ValueSource(ints = {300})
        public void testWindow(Integer size){
            //修改当前窗口大小
            chromeDriver.manage().window().setSize(new Dimension(size, size));
        }
        @Order(3)
        @Test
    public void testAlert() throws InterruptedException {
            WebElement element = chromeDriver.findElement(By.cssSelector("#username"));
            WebElement element1 = chromeDriver.findElement(By.cssSelector("#password"));
            WebElement element2 = chromeDriver.findElement(By.cssSelector("#submit"));
            element.clear();
            element1.clear();
            element.sendKeys("name");
            element1.sendKeys("password");
            element2.click();
            //转化句柄
            chromeDriver.switchTo().alert();
            //阻塞否则可能不能识别alert
            Thread.sleep(1000);
            System.out.println(chromeDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText());
        }



}
